package com.hshoesmall.demo.service.lmpl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hshoesmall.demo.common.exception.ApiAsserts;
import com.hshoesmall.demo.jwt.JwtUtil;
import com.hshoesmall.demo.mapper.ShoesUserMapper;
import com.hshoesmall.demo.model.dto.ShoesUserLoginDTO;
import com.hshoesmall.demo.model.dto.ShoesUserRegisterDTO;
import com.hshoesmall.demo.model.entity.ShoesUser;
import com.hshoesmall.demo.service.ShoesUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;


@Slf4j
//该注解的作用主要是操作在idea中的控制台中打印的日志信息。该注解相当于代替了以下的代码：private  final Logger logger = LoggerFactory.getLogger(当前类名.class);
@Transactional(rollbackFor = Exception.class) //@Transactional注解在代码执行出错的时候能够进行事务的回滚
@Service//@Service服务层组件，用于标注业务层组件,表示定义一个bean，自动根据bean的类名实例化一个首写字母为小写的bean
public class ShoesUserServicelmpl extends ServiceImpl<ShoesUserMapper, ShoesUser> implements ShoesUserService {
//ServiceImpl类是我们进行SQL操作中非常重要的一个类，通过MybatisPlus生成的各个实体类的XXXImpl都会继承ServiceImpl类那里继承全部的方法
// ServiceImpl<M extends BaseMapper<T>, T> implements IService<T> IService 实现类（ 泛型：M 是 mapper 对象，T 是实体 ）

    @Override
    public ShoesUser executeRegister(ShoesUserRegisterDTO dto) {
        LambdaQueryWrapper<ShoesUser> wrapper = new LambdaQueryWrapper<>();
        //LambdaQueryWrapper的好处是写实体属性，而不是数据库字段，这样就不用担心数据库字段改了需要修改java中的代码（只需要修改xml文件就行了）
        wrapper.eq(ShoesUser::getUaccount, dto.getUaccount()).or().eq(ShoesUser::getUemail, dto.getUemail());
        ShoesUser shoesUser = baseMapper.selectOne(wrapper);
        if (!ObjectUtils.isEmpty(shoesUser)){
            ApiAsserts.fail("账号或邮箱已存在！");
        }
        ShoesUser addUser = ShoesUser.builder()
                .uaccount(dto.getUaccount())
                .upwd(dto.getUpwd())
                .upwdans(dto.getUpwdans())
                .uemail(dto.getUemail())
                .uid(0)
                .udelete(false)
                .ugender("")
                .upwdask("")
                .uremarks("")
                .utel("")
                .uname("")
                .uinfo("")
                .uintegral(0)
                .uregtime("")
                .build();
        baseMapper.insert(addUser);
        return addUser;
    }

    @Override
    public ShoesUser getUserByUsername(String uaccount) {
        return baseMapper.selectOne(new LambdaQueryWrapper<ShoesUser>().eq(ShoesUser::getUaccount, uaccount));
    }

    @Override
    public String executeLogin(ShoesUserLoginDTO dto) {
        String token = null;
        try{
            ShoesUser user = getUserByUsername(dto.getUaccount());
            String encodePwd = dto.getUpwd();
            if (!encodePwd.equals(user.getUpwd())){
                throw new Exception("密码错误");
            }
            token = JwtUtil.generateToken(String.valueOf(user.getUaccount()));
        }catch (Exception e){
            log.warn("用户不存在or密码验证失败======>{}", dto.getUaccount());
        }
        return token;
    }
}
